<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>角色新增</title>
	<jsp:include page="/common/meta.jsp"></jsp:include>
	<jsp:include page="/common/resource.jsp"></jsp:include>
</head>
<script type="text/javascript">
/** 关闭窗口*/
function dialogClose() {
	//关闭自己
	if ($.easyui.showDialog.parent.doSearch) {
		$.easyui.showDialog.parent.doSearch();
	} else {
		$.easyui.showDialog.parent.location.reload();
		//刷新父页面
	}
	$.easyui.showDialog.parent.easyuiDialog.close();
}
    /** 初始化菜单树*/
    $(function() {
        createModulTree();
    });
    /** 加载模块树*/
    function createModulTree() {
        $('#moduleTree').tree({
            url : '${base}/system/role/RoleController/querSelectedModule?id=-1',
            method : 'GET',
            animate : true,
            lines : true,
            checkbox : true,
            cascadeCheck : false,
            onClick : function(node) {
                $(this).tree("options").cascadeCheck = true;
            },
            onCheck : function(node) {
                $(this).tree("options").cascadeCheck = true;
            },
            onBeforeExpand : function(node) {
                if (node.children.length > 0) {
                    return true;
                } else {
                    $(this).tree("update", {
                        target : node.target,
                        iconCls : 'tree-file'
                    });
                    return false;
                }
            },
            onLoadSuccess : function() {
                var rootNode = $(this).tree('getRoot');
                $(this).tree("collapseAll");
                $(this).tree('expand', rootNode.target);
            }
        });

    }
    /** 获取选中的模块ID*/
    function getChecked(type) {
        var nodes = $('#moduleTree').tree('getChecked', [ 'checked', 'indeterminate' ]);
        // var nodes = $('#moduleTree').tree('getSelected');
        var s = '';
        if (type == 'id') {
            for (var i = 0; i < nodes.length; i++) {
                if (s != '')
                    s += ',';
                s += nodes[i].id;
            }
        } else if (type == 'text') {
            for (var i = 0; i < nodes.length; i++) {

                if (s != '')
                    s += ',';
                s += nodes[i].text;
            }
        }
        return s;
    }

    /** 保存方法*/
    function doSave() {
        /**验证数据**/
        if (!ValidateForm("#Role_add_form")) {
            return;
        }
        $("#module_ids").val(getChecked('id'));
        $("#module_names").val(getChecked('text'));
        var module_ids = $("#module_ids").val();
        if (module_ids == null || module_ids == '') {
            top.$.messager.alert('提示', '请选择角色权限', 'info');
            return;
        }
        $.ajax({
            url : "${base}/system/role/RoleController/save",
            data : $("#Role_add_form").serialize(),
            type : "POST",
            dataType : "JSON",
            cache : false,
            beforeSend: function (xhr) {
                /** 开启遮罩层次*/
                $.easyui.loading({ msg: "请稍候...",topMost :true });
            },
            complete: function (xhr, ts) {
                /** 关闭遮罩层*/
                $.easyui.loaded({topMost :true});
            },
            success : function(data) {
                var flag = data.flag;
                if (flag) { //验证成功
                    top.$.messager.alert('提示', "保存成功", 'info', function() {
                        dialogClose();
                    });
                } else { //验证失败
                    var msg = data.msg;
                    top.$.messager.alert('保存失败', msg, 'error');
                }
            }
        });
    }
</script>
<body>
<!-- form-box begin -->
<div id="Role_add_div" class="form-box" style="padding-top:10px;height: 85% " >
		<form id="Role_add_form">
			<jsp:include page="vo/addVO.jsp"></jsp:include>
		</form>
	</div>
<!-- form-box end -->
<div id="divToolbar"  style=" margin-right :10px;  text-align: right">
		<a href="javascript:void(0)"  class="easyui-linkbutton button-blue l-btn l-btn-small" onclick="doSave()">保存</a>
		<a href="javascript:void(0)" class="easyui-linkbutton button-red l-btn l-btn-small" onclick="dialogClose()">关闭</a>
	</div>
</body>
</html>
<script type="text/javascript">
    $(function(){
        $.fn.form.methods.enableAutoFocus($("#Role_add_div"));
    });
</script>